/**
 * Definition for a binary tree node.
 * function TreeNode(val, left, right) {
 *     this.val = (val===undefined ? 0 : val)
 *     this.left = (left===undefined ? null : left)
 *     this.right = (right===undefined ? null : right)
 * }
 */
/**
 * @param {TreeNode} p
 * @param {TreeNode} q
 * @return {boolean}
 */
// 比较两颗二叉树相等：相比较根节点，然后比较左子树，右子树
// 用BFS怎么做？
var isSameTree = function(p, q) {
    const q1 = []
    let f1 = 0, r1 = 1
    q1.push(p)
    while(f1 < r1) {
        let node = q1[f1++]
        if(node) {
            q1[r1++] = node.left
            q1[r1++] = node.right
        }
    }
    const q2 = []
    let f2 = 0, r2 = 1
    q2.push(q)
    while(f2 < r2) {
        let node = q2[f2++]
        if(node) {
            q2[r2++] = node.left
            q2[r2++] = node.right
        }
    }
    if(q1.length !== q2.length) return false
    for(let i = 0; i < q1.length; i++) {
        if(q1[i] === null && q2[i] !== null) return false
        if(q2[i] === null && q1[i] !== null) return false
        if(q1[i] == null && q2[i] == null) continue
        if(q1[i].val !== q2[i].val) return false
    }
    return true
};